package bairui.international.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianjing.sh.project.common.service.Service_Number;
import com.sh_lianjing.state.StateTransition;

import bairui.international.model.domain.jpa.TB_Admin;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.domain.jpa.TB_UserAgentRequestOrder;
import bairui.international.model.status.State_UserAgentRequestOrder;
import bairui.international.model.type.Type_User;
import bairui.international.service.Service_User;
import bairui.international.service.Service_UserAgentRequestOrder;
import bairui.international.service.Service_UserAgentRequestOrderRecord;

/**
 * 
 * 【(用户代理商申请单)TB_UserAgentRequestOrder】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_UserAgentRequestOrder 模型
 * @see Service_UserAgentRequestOrder 服务接口
 * @see BService_UserAgentRequestOrderImpl 服务实现基础
 *
 */
@Service
public class Service_UserAgentRequestOrderImpl extends BService_UserAgentRequestOrderImpl implements Service_UserAgentRequestOrder {

	@Autowired private Service_UserAgentRequestOrderRecord service_UserAgentRequestOrderRecord;
	@Autowired private Service_Number service_Number;
	@Autowired private Service_User service_User;
	
	private TB_UserAgentRequestOrder createInstance(TB_User user, String nickName, String name, String phone, String company, String address){
		return dao.save(new TB_UserAgentRequestOrder(
				new TB_UserAgentRequestOrder.MustProperties() {
					@Override public TB_User getUser() {
						return user;
					}
					@Override public Date getSubmitDate() {
						return new Date();
					}
					@Override public State_UserAgentRequestOrder getState() {
						return State_UserAgentRequestOrder.t_create().to();
					}
					@Override public String getPhone() {
						return phone;
					}
					@Override public String getNumber() {
						return service_Number.generateDateFormatNumber("UserAgentRequestOrderImplNumber");
					}
					@Override public String getName() {
						return name;
					}
				},
				new TB_UserAgentRequestOrder.OptionalSingleProperties() {
					@Override public Date getVerifyDate() {
						return null;
					}
					@Override public TB_Admin getVerifior() {
						return null;
					}
					@Override public String getNickName() {
						return nickName;
					}
					@Override public String getCompany() {
						return company;
					}
					@Override public String getAddress() {
						return address;
					}
				}
			));
	}
	
	@Override
	public TB_UserAgentRequestOrder create(String skey, String name, String phone, String company, String address){
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
			user.setRealName(name);
			user.setPhone(phone);
			user.setCompany(company);
		// 
		TB_UserAgentRequestOrder order = createInstance(user, user.getNickName(), name, phone, company, address);
		// 记录
		service_UserAgentRequestOrderRecord.createFlowRecord(order, order.getState().getComment());
		//
		return order;
	}
	
	@Override
	public TB_UserAgentRequestOrder vertifyPass(long id) throws Exception {
		TB_UserAgentRequestOrder order = findById(id);
		// 更新信息
		StateTransition<State_UserAgentRequestOrder> trans = order.new UpdateProperties() {
			StateTransition<State_UserAgentRequestOrder> trans;
			@Override public State_UserAgentRequestOrder getState(TB_UserAgentRequestOrder it) throws Exception {
				trans = it.getState().t_verifyPass_from_Verifing();
				return trans.to();
			};
			@Override public Date getVerifyDate(TB_UserAgentRequestOrder it) throws Exception {
				return new Date();
			};
		}.trans;
		// 记录
		service_UserAgentRequestOrderRecord.createPassRecord(order, trans.comment(), "");
		// 设置为经销商
		TB_User user = order.getUser();
		user.new UpdateProperties() {
			@Override
			public Type_User getType(TB_User it) throws Exception {
				return Type_User.Agent;
			}
		};
		//
		return order;
	}

	@Override
	public TB_UserAgentRequestOrder vertifyNoPass(long id, String message) throws Exception {
		TB_UserAgentRequestOrder order = findById(id);
		// 更新信息
		StateTransition<State_UserAgentRequestOrder> trans = order.new UpdateProperties() {
			StateTransition<State_UserAgentRequestOrder> trans;
			@Override public State_UserAgentRequestOrder getState(TB_UserAgentRequestOrder it) throws Exception {
				trans = it.getState().t_verifyNoPass_from_Verifing();
				return trans.to();
			};
			@Override public Date getVerifyDate(TB_UserAgentRequestOrder it) throws Exception {
				return new Date();
			};
		}.trans;
		// 记录
		service_UserAgentRequestOrderRecord.createNoPassRecord(order, trans.comment(), message);
		//
		return order;
	}
	
}
